package com.yilong.wei.app.controller;

import com.yilong.wei.app.domain.AreaData;
import com.yilong.wei.app.service.AreaDataService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.Currency;

/**
 * @program: wei
 * @description: 全国小区编码解析
 * @author: SOS
 * @create: 2019-07-05 11:04
 **/
@RestController
public class AreaDataController {

    @Autowired
    private AreaDataService areaDataService;

    @RequestMapping("selectByPrimaryKey")
    public AreaData selectByPrimaryKey(@RequestParam(value = "id") String id) {
        AreaData areaData = areaDataService.selectByPrimaryKey(id);
        return areaData;
    }

    @RequestMapping("insert")
    public String insertAreaData() {
        try {
            File file = new File("F:/data/cmpare/贵州省(2).txt");
            String string = FileUtils.readFileToString(file, "UTF-8");
            String[] split = string.replaceAll("\\r|\\n", "").split(";");
            for (int i = 0; i < split.length; i++) {
                String[] split1 = split[i].split(",");
                AreaData areaData = new AreaData();
                areaData.setAreaCodeId(split1[0].trim());
                areaData.setType(split1[1]);
                areaData.setParentId((Long.parseLong(split1[0]) / 1000 * 1000) + "");
                areaData.setAreaName(split1[2]);
                int mun = areaDataService.insert(areaData);
                if (mun > 0) {

                } else {
                    System.out.println(split1[0] + "插入失败");
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "success";
    }

    @RequestMapping("mergeData")
    public String mergeData(@RequestParam String dir) {
        long start = System.currentTimeMillis();
        int add = 0;
        int update = 0;
        try {
            File file1 = new File("F:\\data\\" + dir);
            File[] list = file1.listFiles();
            if (list != null) {
                for (File file : list) {
                    String string = FileUtils.readFileToString(file, "UTF-8");
                    String[] split = string.replaceAll("\\r|\\n", "").split(";");
                    for (String s : split) {
                        String[] split1 = s.split(",");
                        AreaData areaData1 = areaDataService.selectByPrimaryKey(split1[0].trim());
                        if (areaData1 == null) {
                            AreaData areaData = new AreaData();
                            areaData.setAreaCodeId(split1[0].trim());
                            areaData.setType(split1[1]);
                            areaData.setParentId((Long.parseLong(split1[0]) / 1000 * 1000) + "");
                            areaData.setAreaName(split1[2]);
                            int mun = areaDataService.insert(areaData);
                            if (mun > 0) {
                                add++;
                                System.out.println(split1[0] + "新增成功.");
                            } else {
                                System.out.println(split1[0] + "新增失败.");
                            }
                        } else {
                            AreaData areaData = new AreaData();
                            areaData.setAreaCodeId(split1[0].trim());
                            areaData.setType(split1[1]);
                            areaData.setParentId((Long.parseLong(split1[0]) / 1000 * 1000) + "");
                            areaData.setAreaName(split1[2]);
                            int mun = areaDataService.updateByPrimaryKey(areaData);
                            if (mun > 0) {
                                update++;
                                System.out.println(split1[0] + "更新成功.");
                            } else {
                                System.out.println(split1[0] + "更新失败.");
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        return "success,耗时:" + (end - start) + "新增:" + add + ",更新:" + update;
    }

    @RequestMapping("mergeDataParent")
    public String mergeDataParent(@RequestParam String dir) {
        long start = System.currentTimeMillis();
        int add = 0;
        int update = 0;
        try {
            File file1 = new File("F:\\data\\" + dir);
            File[] list = file1.listFiles();
            if (list != null) {
                for (File file : list) {
                    String string = FileUtils.readFileToString(file, "UTF-8");
                    String[] split = string.split(";");
                    for (String s : split) {
                        String[] split1 = s.split(",");
                        AreaData areaData1 = areaDataService.selectByPrimaryKey(split1[0].trim());
                        if (areaData1 == null) {
                            AreaData areaData = new AreaData();
                            areaData.setAreaCodeId(split1[0].trim());
                            areaData.setType("111");
                            areaData.setParentId(split1[1]);
                            areaData.setAreaName(split1[2]);
                            int mun = areaDataService.insert(areaData);
                            if (mun > 0) {
                                add++;
                                System.out.println(split1[0] + "新增成功.");
                            } else {
                                System.out.println(split1[0] + "新增失败.");
                            }
                        } else {
                            AreaData areaData = new AreaData();
                            areaData.setAreaCodeId(split1[0].trim());
                            areaData.setType("111");
                            areaData.setParentId(split1[1]);
                            areaData.setAreaName(split1[2]);
                            int mun = areaDataService.updateByPrimaryKey(areaData);
                            if (mun > 0) {
                                update++;
                                System.out.println(split1[0] + "更新成功.");
                            } else {
                                System.out.println(split1[0] + "更新失败.");
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        return "success,耗时:" + (end - start) + "新增:" + add + ",更新:" + update;
    }

    public static void main(String[] args) {
        try {
            String dir = "167";
            File file1 = new File("F:\\data\\" + dir);
            File[] list = file1.listFiles();
            if (list != null) {
                for (int i = 0; i < 1; i++) {
                    String s = FileUtils.readFileToString(list[i], "UTF-8");
                    String[] split = s.replaceAll("\\r|\\n", "").split(";");
                    for (String s1 : split) {
                        System.out.println(s1);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
